#include "Sqstack.h"
//初始化
void Initsqstack(sqstack& s)
{
	s.base = new SElemType[SIZE];
	if (!s.base)
	{
		exit(OVERFLOW);
	}
	s.top = s.base;
	s.stacksize = SIZE;
}
//入栈操作
void pushstack_sq(sqstack& s, SElemType e)
{
	if (s.top - s.base == s.stacksize)
	{
		cout << "栈已满！" << endl;

	}
	else
	{
		//*s.top++ = e;
		*s.top = e;
		s.top++;
	}
}
//出栈操作
void popstack_sq(sqstack& s, SElemType& e)
{
	if (s.base == s.top)
	{
		cout << "栈内是空的！" << endl;
	}
	else
		e = *(--s.top);
}
//十进制数转换为其他进制数的操作
void shuzhizhuanhuan()
{
	sqstack s; Initsqstack(s);
	int num, b; cout << "请输入一个十进制数：";
	cin >> num;
	cout << "请输入要转换的进制数：";
	cin >> b;
	cout << "转换结果为：";
	while (num)
	{
		pushstack_sq(s, num% b);
		num /= b;
	}

	while (s.base != s.top)
	{
		SElemType e;
		popstack_sq(s, e);
		cout << e;
	}
}
// 判断栈是否为空
bool stackempty(sqstack& s)
{
	return s.base == s.top;
}
void kuohaomatch(char* exp)
{
	sqstack s;
	Initsqstack(s);
	int state = 1, i = 0;
	SElemType x;
	while (exp[i] != '\0' && state)
	{
		switch (exp[i])
		{
		case '(': pushstack_sq(s, exp[i]); i++;
			break;
		case ')':
			if (!stackempty(s))
			{
				popstack_sq(s, x);
				i++;
			}
			else
			{
				state = 0;
			}
			break;
		case ' ':
			i++; break; // 处理空格，跳过
		default: i++; break; // 忽略其他字符，跳过
		}
	}
	if (stackempty(s) && state)
	{
		cout << "匹配成功!" << endl;
	}
	else
	{
		cout << "匹配失败！" << endl;
	}
}
